<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>xv6 实验3 page tables | kiloGrand</title><meta name="keywords" content="xv6"><meta name="author" content="kiloGrand"><meta name="copyright" content="kiloGrand"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Lab pgtbl: Page tables https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.S081&#x2F;2020&#x2F;schedule.html https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.S081&#x2F;2020&#x2F;labs&#x2F;pgtbl.html  Print a page table本小节参考freewalk的代码，实现RISC-V三级页表翻译，打印页表的内容。 在kernel&#x2F;d">
<meta property="og:type" content="article">
<meta property="og:title" content="xv6 实验3 page tables">
<meta property="og:url" content="https://kilogrand.gitee.io/2022/09/03/xv6-lab3/index.html">
<meta property="og:site_name" content="kiloGrand">
<meta property="og:description" content="Lab pgtbl: Page tables https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.S081&#x2F;2020&#x2F;schedule.html https:&#x2F;&#x2F;pdos.csail.mit.edu&#x2F;6.S081&#x2F;2020&#x2F;labs&#x2F;pgtbl.html  Print a page table本小节参考freewalk的代码，实现RISC-V三级页表翻译，打印页表的内容。 在kernel&#x2F;d">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://kilogrand.gitee.io/img/coding.jpg">
<meta property="article:published_time" content="2022-09-03T01:41:10.000Z">
<meta property="article:modified_time" content="2022-10-18T15:55:06.451Z">
<meta property="article:author" content="kiloGrand">
<meta property="article:tag" content="xv6">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://kilogrand.gitee.io/img/coding.jpg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://kilogrand.gitee.io/2022/09/03/xv6-lab3/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":false,"languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'xv6 实验3 page tables',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-10-18 23:55:06'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.2"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/profile.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('/img/coding.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">kiloGrand</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">xv6 实验3 page tables</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-09-03T01:41:10.000Z" title="发表于 2022-09-03 09:41:10">2022-09-03</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-10-18T15:55:06.451Z" title="更新于 2022-10-18 23:55:06">2022-10-18</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/">操作系统</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">6.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>31分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="xv6 实验3 page tables"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Lab-pgtbl-Page-tables"><a href="#Lab-pgtbl-Page-tables" class="headerlink" title="Lab pgtbl: Page tables"></a>Lab pgtbl: Page tables</h1><ul>
<li><a target="_blank" rel="noopener" href="https://pdos.csail.mit.edu/6.S081/2020/schedule.html">https://pdos.csail.mit.edu/6.S081/2020/schedule.html</a></li>
<li><a target="_blank" rel="noopener" href="https://pdos.csail.mit.edu/6.S081/2020/labs/pgtbl.html">https://pdos.csail.mit.edu/6.S081/2020/labs/pgtbl.html</a></li>
</ul>
<h2 id="Print-a-page-table"><a href="#Print-a-page-table" class="headerlink" title="Print a page table"></a>Print a page table</h2><p>本小节参考freewalk的代码，实现RISC-V三级页表翻译，打印页表的内容。</p>
<p>在<code>kernel/defs.h</code>声明<code>void vmprint(pagetable_t);</code></p>
<p>在<code>kernel/exec.c</code>的<code>return argc;</code>之前添加<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (p-&gt;pid == <span class="number">1</span>) &#123;</span><br><span class="line">	vmprint(p-&gt;pagetable);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>那么，在os启动的时候，会打印页表的内容。</p>
<p>参考<code>kernel/vm.c</code>中<code>freewalk(pagetable_t pagetable)</code>函数访问页表的方式递归输出页表信息。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Recursively free page-table pages.</span></span><br><span class="line"><span class="comment">// All leaf mappings must already have been removed.</span></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">freewalk</span><span class="params">(<span class="type">pagetable_t</span> pagetable)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// there are 2^9 = 512 PTEs in a page table.</span></span><br><span class="line">  <span class="keyword">for</span>(<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">512</span>; i++)&#123;</span><br><span class="line">    <span class="type">pte_t</span> pte = pagetable[i];</span><br><span class="line">    <span class="keyword">if</span>((pte &amp; PTE_V) &amp;&amp; (pte &amp; (PTE_R|PTE_W|PTE_X)) == <span class="number">0</span>)&#123;</span><br><span class="line">      <span class="comment">// this PTE points to a lower-level page table.</span></span><br><span class="line">      uint64 child = PTE2PA(pte);</span><br><span class="line">      freewalk((<span class="type">pagetable_t</span>)child);</span><br><span class="line">      pagetable[i] = <span class="number">0</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> <span class="keyword">if</span>(pte &amp; PTE_V)&#123;</span><br><span class="line">      panic(<span class="string">&quot;freewalk: leaf&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  kfree((<span class="type">void</span>*)pagetable);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p><a target="_blank" rel="noopener" href="https://kilogrand.github.io/2022/08/31/xv6-Chapter-3/">RISC-V的页表</a><br><img src="/img/Figure-3.2.jpg" alt="Figure-3.2"></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vmprint辅助函数</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span> </span><br><span class="line">_vmprint(<span class="type">pagetable_t</span> pagetable, <span class="type">int</span> level)</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">512</span>; ++i) &#123; <span class="comment">// 每一页都包含 512 个 PTE，用于指向下一个页表或物理地址</span></span><br><span class="line">    <span class="type">pte_t</span> pte = pagetable[i];</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> ((pte &amp; PTE_V)) &#123; <span class="comment">// PTE_V 表示 PTE 是否有效</span></span><br><span class="line"></span><br><span class="line">      <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; level; ++j) &#123; <span class="comment">// 根据现在是L2、L1、L0 (level: 1,2,3)来打印，形成树形结构</span></span><br><span class="line">        <span class="keyword">if</span> (j == <span class="number">0</span>) <span class="built_in">printf</span>(<span class="string">&quot;..&quot;</span>);</span><br><span class="line">        <span class="keyword">else</span> <span class="built_in">printf</span>(<span class="string">&quot; ..&quot;</span>);</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      uint64 child = PTE2PA(pte); <span class="comment">// 通过pte映射下一级页表的物理地址</span></span><br><span class="line">      <span class="built_in">printf</span>(<span class="string">&quot;%d: pte %p pa %p\n&quot;</span>, i, pte, child);</span><br><span class="line">      <span class="comment">// 判断是否在最后一级页表，若不是，则递归</span></span><br><span class="line">      <span class="comment">// 只有在页表的最后一级，才可进行读、写、执行</span></span><br><span class="line">      <span class="keyword">if</span> ((pte &amp; (PTE_R | PTE_W | PTE_X)) == <span class="number">0</span>)</span><br><span class="line">        _vmprint((<span class="type">pagetable_t</span>)child, level + <span class="number">1</span>);  <span class="comment">// 层级加1</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 打印页表的内容</span></span><br><span class="line"><span class="type">void</span> </span><br><span class="line"><span class="title function_">vmprint</span><span class="params">(<span class="type">pagetable_t</span> pagetable)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="built_in">printf</span>(<span class="string">&quot;page table %p\n&quot;</span>, pagetable);</span><br><span class="line">  _vmprint(pagetable, <span class="number">1</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="A-kernel-page-table-per-process"><a href="#A-kernel-page-table-per-process" class="headerlink" title="A kernel page table per process"></a>A kernel page table per process</h2><blockquote>
<p>当前，xv6的内核态只有一个直接映射到物理内存的page table，并且这个页表没有用户态进程的地址信息。<br>因此用户态向内核态传递地址时，先在用户态将虚拟地址转为物理地址，然后把物理地址通过寄存器传递给内核。</p>
<p>在lab2中，我们在结构体<code>struct proc</code>添加了一个成员变量<code>int mast</code>，实现<code>sys_trace</code>；<br>在本小节中，我们在结构体<code>struct proc</code>添加一个成员变量<code>kernel_pagetable</code>，<br>为每个进程添加一个内核态的页表，然后在该进程进入到内核前，<br>将该进程的kernel page table的物理地址写入SATP寄存器(Supervisor Address Translation and Protection Register)，即使用该进程的内核态页表，<br>这样就可以实现<strong>在内核态直接翻译虚拟地址的功能</strong>。</p>
</blockquote>
<hr>
<h3 id="一个内核页表的创建"><a href="#一个内核页表的创建" class="headerlink" title="一个内核页表的创建"></a>一个内核页表的创建</h3><ul>
<li>在<code>kernel/proc.h</code>中的<code>struct proc</code>添加新成员变量<code>pagetable_t kernel_pagetable</code>。</li>
<li>首先查看kernel/vm.c中的kvminit函数。该函数通过kalloc为内核创建一个空的页表，然后通过kvmmap完成直接映射操作。</li>
<li>现在要为每个新进程生成一个内核页表，所以需要参考kvminit重新实现一个为进程生成内核页表的版本</li>
</ul>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * the kernel&#x27;s page table.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">pagetable_t</span> kernel_pagetable;</span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> etext[];  <span class="comment">// kernel.ld sets this to end of kernel code.</span></span><br><span class="line"><span class="keyword">extern</span> <span class="type">char</span> trampoline[]; <span class="comment">// trampoline.S</span></span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment"> * create a direct-map page table for the kernel.</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">kvminit</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  kernel_pagetable = (<span class="type">pagetable_t</span>) kalloc();</span><br><span class="line">  <span class="built_in">memset</span>(kernel_pagetable, <span class="number">0</span>, PGSIZE);</span><br><span class="line">  <span class="comment">// uart registers</span></span><br><span class="line">  kvmmap(UART0, UART0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  <span class="comment">// virtio mmio disk interface</span></span><br><span class="line">  kvmmap(VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  <span class="comment">// CLINT</span></span><br><span class="line">  kvmmap(CLINT, CLINT, <span class="number">0x10000</span>, PTE_R | PTE_W);</span><br><span class="line">  <span class="comment">// PLIC</span></span><br><span class="line">  kvmmap(PLIC, PLIC, <span class="number">0x400000</span>, PTE_R | PTE_W);</span><br><span class="line">  <span class="comment">// map kernel text executable and read-only.</span></span><br><span class="line">  kvmmap(KERNBASE, KERNBASE, (uint64)etext-KERNBASE, PTE_R | PTE_X);</span><br><span class="line">  <span class="comment">// map kernel data and the physical RAM we&#x27;ll make use of.</span></span><br><span class="line">  kvmmap((uint64)etext, (uint64)etext, PHYSTOP-(uint64)etext, PTE_R | PTE_W);</span><br><span class="line">  <span class="comment">// map the trampoline for trap entry/exit to</span></span><br><span class="line">  <span class="comment">// the highest virtual address in the kernel.</span></span><br><span class="line">  kvmmap(TRAMPOLINE, (uint64)trampoline, PGSIZE, PTE_R | PTE_X);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// add a mapping to the kernel page table.</span></span><br><span class="line"><span class="comment">// only used when booting.</span></span><br><span class="line"><span class="comment">// does not flush TLB or enable paging.</span></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">kvmmap</span><span class="params">(uint64 va, uint64 pa, uint64 sz, <span class="type">int</span> perm)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">if</span>(mappages(kernel_pagetable, va, sz, pa, perm) != <span class="number">0</span>)</span><br><span class="line">    panic(<span class="string">&quot;kvmmap&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">ukvmmap</span><span class="params">(<span class="type">pagetable_t</span> pagetable, uint64 va, uint64 pa, uint64 sz, <span class="type">int</span> perm)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">if</span>(mappages(pagetable, va, sz, pa, perm) != <span class="number">0</span>)</span><br><span class="line">    panic(<span class="string">&quot;ukvmmap&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">pagetable_t</span></span><br><span class="line"><span class="title function_">proc_kvminit</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// 申请一个页表空间</span></span><br><span class="line">  <span class="type">pagetable_t</span> proc_kernel_pagetable = (<span class="type">pagetable_t</span>) kalloc();</span><br><span class="line">  <span class="keyword">if</span> (proc_kernel_pagetable == <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  <span class="built_in">memset</span>(proc_kernel_pagetable, <span class="number">0</span>, PGSIZE);</span><br><span class="line">  <span class="comment">// 与vminint内容上保持一致</span></span><br><span class="line">  ukvmmap(proc_kernel_pagetable, UART0, UART0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, CLINT, CLINT, <span class="number">0x10000</span>, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, PLIC, PLIC, <span class="number">0x400000</span>, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, KERNBASE, KERNBASE, (uint64)etext-KERNBASE, PTE_R | PTE_X);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, (uint64)etext, (uint64)etext, PHYSTOP-(uint64)etext, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, TRAMPOLINE, (uint64)trampoline, PGSIZE, PTE_R | PTE_X);</span><br><span class="line">  <span class="keyword">return</span> proc_kernel_pagetable;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>同时，在<code>kernel/defs.h</code>里面添加这两个函数的声明。</p>
<h3 id="为每个新进程生成一个内核页表"><a href="#为每个新进程生成一个内核页表" class="headerlink" title="为每个新进程生成一个内核页表"></a>为每个新进程生成一个内核页表</h3><blockquote>
<p>确保每个进程的内核页表都有该进程的内核栈的映射。<br>在未修改的xv6中，所有内核栈都在procinit中设置。<br>你需要将部分或全部功能移至allocproc。</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// kernel/proc.c void procinit(void)</span></span><br><span class="line"><span class="comment">// initialize the proc table at boot time.</span></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">procinit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">proc</span> *<span class="title">p</span>;</span></span><br><span class="line">  </span><br><span class="line">  initlock(&amp;pid_lock, <span class="string">&quot;nextpid&quot;</span>);</span><br><span class="line">  <span class="keyword">for</span>(p = proc; p &lt; &amp;proc[NPROC]; p++) &#123;</span><br><span class="line">      initlock(&amp;p-&gt;lock, <span class="string">&quot;proc&quot;</span>);</span><br><span class="line">  <span class="comment">// 删除以下部分，将内核栈的空间申请和映射放在创建进程时</span></span><br><span class="line">  <span class="comment">//     // Allocate a page for the process&#x27;s kernel stack.</span></span><br><span class="line">  <span class="comment">//     // Map it high in memory, followed by an invalid</span></span><br><span class="line">  <span class="comment">//     // guard page.</span></span><br><span class="line">  <span class="comment">//     char *pa = kalloc();</span></span><br><span class="line">  <span class="comment">//     if(pa == 0)</span></span><br><span class="line">  <span class="comment">//       panic(&quot;kalloc&quot;);</span></span><br><span class="line">  <span class="comment">//     uint64 va = KSTACK((int) (p - proc));</span></span><br><span class="line">  <span class="comment">//     kvmmap(va, (uint64)pa, PGSIZE, PTE_R | PTE_W);</span></span><br><span class="line">  <span class="comment">//     p-&gt;kstack = va;</span></span><br><span class="line">  &#125;</span><br><span class="line">  kvminithart();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Look in the process table for an UNUSED proc.</span></span><br><span class="line"><span class="comment">// If found, initialize state required to run in the kernel,</span></span><br><span class="line"><span class="comment">// and return with p-&gt;lock held.</span></span><br><span class="line"><span class="comment">// If there are no free procs, or a memory allocation fails, return 0.</span></span><br><span class="line"><span class="type">static</span> <span class="keyword">struct</span> proc*</span><br><span class="line"><span class="title function_">allocproc</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">proc</span> *<span class="title">p</span>;</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span>(p = proc; p &lt; &amp;proc[NPROC]; p++) &#123;</span><br><span class="line">    acquire(&amp;p-&gt;lock);</span><br><span class="line">    <span class="keyword">if</span>(p-&gt;state == UNUSED) &#123;</span><br><span class="line">      <span class="keyword">goto</span> found;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      release(&amp;p-&gt;lock);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">found:</span><br><span class="line">  p-&gt;pid = allocpid();</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Allocate a trapframe page.</span></span><br><span class="line">  <span class="keyword">if</span>((p-&gt;trapframe = (<span class="keyword">struct</span> trapframe *)kalloc()) == <span class="number">0</span>)&#123;</span><br><span class="line">    release(&amp;p-&gt;lock);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// An empty user page table.</span></span><br><span class="line">  p-&gt;pagetable = proc_pagetable(p);</span><br><span class="line">  <span class="keyword">if</span>(p-&gt;pagetable == <span class="number">0</span>)&#123;</span><br><span class="line">    freeproc(p);</span><br><span class="line">    release(&amp;p-&gt;lock);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建进程时，为进程分配独立的内核页表</span></span><br><span class="line">  p-&gt;kernel_pagetable = proc_kvminit();</span><br><span class="line">  <span class="keyword">if</span> (p-&gt;kernel_pagetable == <span class="number">0</span>) &#123;</span><br><span class="line">    freeproc(p);</span><br><span class="line">    release(&amp;p-&gt;lock);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 初始化当前内核页表的内核栈</span></span><br><span class="line">  <span class="type">char</span> *pa = kalloc();</span><br><span class="line">  <span class="keyword">if</span> (pa == <span class="number">0</span>)</span><br><span class="line">    panic(<span class="string">&quot;kalloc&quot;</span>);</span><br><span class="line">  <span class="comment">// 将内核栈映射到用户内核页表固定的部分</span></span><br><span class="line">  uint64 va = KSTACK((<span class="type">int</span>)<span class="number">0</span>);</span><br><span class="line">  <span class="comment">// 添加kernel stack的映射到用户的kernel pagetable中</span></span><br><span class="line">  ukvmmap(p-&gt;kernel_pagetable, va, (uint64)pa, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  p-&gt;kstack = va;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// Set up new context to start executing at forkret,</span></span><br><span class="line">  <span class="comment">// which returns to user space.</span></span><br><span class="line">  <span class="built_in">memset</span>(&amp;p-&gt;context, <span class="number">0</span>, <span class="keyword">sizeof</span>(p-&gt;context));</span><br><span class="line">  p-&gt;context.ra = (uint64)forkret;</span><br><span class="line">  p-&gt;context.sp = p-&gt;kstack + PGSIZE;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> p;</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="切换进程时能够切换内核页表"><a href="#切换进程时能够切换内核页表" class="headerlink" title="切换进程时能够切换内核页表"></a>切换进程时能够切换内核页表</h3><blockquote>
<p>修改scheduler()以便将进程的内核页表加载到内核的satp寄存器中(请参阅kvminithart以获得灵感)。<br>在调用w_satp()之后不要忘记调用sfence_vma()。</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">scheduler</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">proc</span> *<span class="title">p</span>;</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">cpu</span> *<span class="title">c</span> =</span> mycpu();</span><br><span class="line">  </span><br><span class="line">  c-&gt;proc = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">for</span>(;;)&#123;</span><br><span class="line">    <span class="comment">// Avoid deadlock by ensuring that devices can interrupt.</span></span><br><span class="line">    intr_on();</span><br><span class="line">    </span><br><span class="line">    <span class="type">int</span> found = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span>(p = proc; p &lt; &amp;proc[NPROC]; p++) &#123;</span><br><span class="line">      acquire(&amp;p-&gt;lock);</span><br><span class="line">      <span class="keyword">if</span>(p-&gt;state == RUNNABLE) &#123;</span><br><span class="line">        <span class="comment">// Switch to chosen process.  It is the process&#x27;s job</span></span><br><span class="line">        <span class="comment">// to release its lock and then reacquire it</span></span><br><span class="line">        <span class="comment">// before jumping back to us.</span></span><br><span class="line">        p-&gt;state = RUNNING;</span><br><span class="line">        c-&gt;proc = p;</span><br><span class="line">        </span><br><span class="line">        <span class="comment">// 在切换任务前，将用户内核页表替换到stap寄存器中</span></span><br><span class="line">        w_satp(MAKE_SATP(p-&gt;kernel_pagetable));</span><br><span class="line">        <span class="comment">// 清除快表缓存</span></span><br><span class="line">        sfence_vma();</span><br><span class="line">        <span class="comment">// 调度，执行进程</span></span><br><span class="line">        swtch(&amp;c-&gt;context, &amp;p-&gt;context);</span><br><span class="line"></span><br><span class="line">        <span class="comment">// Process is done running for now.</span></span><br><span class="line">        <span class="comment">// It should have changed its p-&gt;state before coming back.</span></span><br><span class="line">        <span class="comment">// 该进程执行结束后，将SATP寄存器的值设置为全局内核页表地址</span></span><br><span class="line">        kvminithart();</span><br><span class="line">        c-&gt;proc = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">        found = <span class="number">1</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      release(&amp;p-&gt;lock);</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">if</span> !defined (LAB_FS)</span></span><br><span class="line">    <span class="keyword">if</span>(found == <span class="number">0</span>) &#123;</span><br><span class="line">      intr_on();</span><br><span class="line">      <span class="keyword">asm</span> <span class="title function_">volatile</span><span class="params">(<span class="string">&quot;wfi&quot;</span>)</span>;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="meta">#<span class="keyword">else</span></span></span><br><span class="line">    ;</span><br><span class="line"><span class="meta">#<span class="keyword">endif</span></span></span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="销毁进程时释放内核页表"><a href="#销毁进程时释放内核页表" class="headerlink" title="销毁进程时释放内核页表"></a>销毁进程时释放内核页表</h3><blockquote>
<p>在freeproc中释放进程的内核页表。</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// free a proc structure and the data hanging from it,</span></span><br><span class="line"><span class="comment">// including user pages.</span></span><br><span class="line"><span class="comment">// p-&gt;lock must be held.</span></span><br><span class="line"><span class="type">static</span> <span class="type">void</span></span><br><span class="line"><span class="title function_">freeproc</span><span class="params">(<span class="keyword">struct</span> proc *p)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">if</span>(p-&gt;trapframe)</span><br><span class="line">    kfree((<span class="type">void</span>*)p-&gt;trapframe);</span><br><span class="line">  p-&gt;trapframe = <span class="number">0</span>;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// 删除内核栈</span></span><br><span class="line">  <span class="keyword">if</span> (p-&gt;kstack) &#123;</span><br><span class="line">    <span class="comment">// 通过页表地址， kstack虚拟地址 找到最后一级的页表项</span></span><br><span class="line">    <span class="type">pte_t</span>* pte = walk(p-&gt;kernel_pagetable, p-&gt;kstack, <span class="number">0</span>);</span><br><span class="line">    <span class="keyword">if</span> (pte == <span class="number">0</span>)</span><br><span class="line">      panic(<span class="string">&quot;freeproc : kstack&quot;</span>);</span><br><span class="line">    <span class="comment">// 删除页表项对应的物理地址</span></span><br><span class="line">    kfree((<span class="type">void</span>*)PTE2PA(*pte));</span><br><span class="line">  &#125;</span><br><span class="line">  p-&gt;kstack = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(p-&gt;pagetable)</span><br><span class="line">    proc_freepagetable(p-&gt;pagetable, p-&gt;sz);</span><br><span class="line">  p-&gt;pagetable = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 删除kernel pagetable</span></span><br><span class="line">  <span class="keyword">if</span> (p-&gt;kernel_pagetable) </span><br><span class="line">    proc_freekernelpagetable(p-&gt;kernel_pagetable);  </span><br><span class="line">  p-&gt;kernel_pagetable = <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line">  p-&gt;sz = <span class="number">0</span>;</span><br><span class="line">  p-&gt;pid = <span class="number">0</span>;</span><br><span class="line">  p-&gt;parent = <span class="number">0</span>;</span><br><span class="line">  p-&gt;name[<span class="number">0</span>] = <span class="number">0</span>;</span><br><span class="line">  p-&gt;chan = <span class="number">0</span>;</span><br><span class="line">  p-&gt;killed = <span class="number">0</span>;</span><br><span class="line">  p-&gt;xstate = <span class="number">0</span>;</span><br><span class="line">  p-&gt;state = UNUSED;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">void</span> </span><br><span class="line"><span class="title function_">proc_freekernelpagetable</span><span class="params">(<span class="type">pagetable_t</span> pagetable)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; <span class="number">512</span>; ++i) &#123;</span><br><span class="line">    <span class="type">pte_t</span> pte = pagetable[i];</span><br><span class="line">    <span class="keyword">if</span> ((pte &amp; PTE_V)) &#123;</span><br><span class="line">      pagetable[i] = <span class="number">0</span>;</span><br><span class="line">      <span class="keyword">if</span> ((pte &amp; (PTE_R | PTE_W | PTE_X)) == <span class="number">0</span>) &#123;</span><br><span class="line">        uint64 child = PTE2PA(pte);</span><br><span class="line">        proc_freekernelpagetable((<span class="type">pagetable_t</span>)child);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  kfree((<span class="type">void</span>*)pagetable);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>需要将 <code>walk</code> 函数的定义添加到 <code>kernel/defs.h</code> 中，否则无法直接引用。</p>
<h3 id="获取进程内核页表"><a href="#获取进程内核页表" class="headerlink" title="获取进程内核页表"></a>获取进程内核页表</h3><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">uint64</span><br><span class="line"><span class="title function_">kvmpa</span><span class="params">(uint64 va)</span></span><br><span class="line">&#123;</span><br><span class="line">  uint64 off = va % PGSIZE;</span><br><span class="line">  <span class="type">pte_t</span> *pte;</span><br><span class="line">  uint64 pa;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//注释掉</span></span><br><span class="line">  <span class="comment">//pte = walk(kernel_pagetable, va, 0);</span></span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">proc</span> *<span class="title">p</span> =</span> myproc();</span><br><span class="line">  pte = walk(p-&gt;kernel_pagetable, va, <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(pte == <span class="number">0</span>)</span><br><span class="line">    panic(<span class="string">&quot;kvmpa&quot;</span>);</span><br><span class="line">  <span class="keyword">if</span>((*pte &amp; PTE_V) == <span class="number">0</span>)</span><br><span class="line">    panic(<span class="string">&quot;kvmpa&quot;</span>);</span><br><span class="line">  pa = PTE2PA(*pte);</span><br><span class="line">  <span class="keyword">return</span> pa+off;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>最后，在vm.c中添加头文件，因为使用了结构体proc。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;spinlock.h&quot;</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&quot;proc.h&quot;</span></span></span><br></pre></td></tr></table></figure>
<h2 id="Simplify-copyin-copyinstr"><a href="#Simplify-copyin-copyinstr" class="headerlink" title="Simplify copyin/copyinstr"></a>Simplify copyin/copyinstr</h2><p>这个任务的目的就是在用户进程内核页表中添加用户页表映射的副本。</p>
<hr>
<p>1.将<code>vm.c</code>中<code>copyin</code>的内容替换为对<code>copyin_new</code>的调用，将<code>vm.c</code>中<code>copyinstr</code>的内容替换为对<code>copyinstr_new</code>的调用。<br><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">copyin</span><span class="params">(<span class="type">pagetable_t</span> pagetable, <span class="type">char</span> *dst, uint64 srcva, uint64 len)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">return</span> copyin_new(pagetable, dst, srcva, len);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">copyinstr</span><span class="params">(<span class="type">pagetable_t</span> pagetable, <span class="type">char</span> *dst, uint64 srcva, uint64 max)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">return</span> copyinstr_new(pagetable, dst, srcva, max);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><br>然后，将<code>copyin_new</code>和<code>copyinstr_new</code>的声明添加到<code>kernel/defs.h</code>中</p>
<hr>
<p>2.在<code>kernel/vm.c</code>中添加<code>kvmcopy</code>和<code>kvmdealloc</code>函数，<br>用户页表的变化同步到用户进程内核页表中，需要实现映射和缩减两个操作。<br>注意权限，需要把<code>PTE_U</code>去掉，因为CPU在<code>suprivisor模式</code>时不能访问设置<code>PTE_U</code>的页。<br>同时，不要释放物理内存，拷贝映射关系即可。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// kernel/vm.c</span></span><br><span class="line"><span class="comment">// 将 src 页表的一部分页映射关系拷贝到 dst 页表中。</span></span><br><span class="line"><span class="comment">// 只拷贝页表项，不拷贝实际的物理页内存。</span></span><br><span class="line"><span class="comment">// 成功返回0，失败返回 -1</span></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">kvmcopy</span><span class="params">(<span class="type">pagetable_t</span> src, <span class="type">pagetable_t</span> dst, uint64 start, uint64 end)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="type">pte_t</span> *pte;</span><br><span class="line">  uint64 pa, i;</span><br><span class="line">  uint flags;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// PGROUNDUP: 对齐页边界，防止 remap</span></span><br><span class="line">  <span class="keyword">for</span>(i = PGROUNDUP(start); i &lt; end; i += PGSIZE)&#123;</span><br><span class="line">    <span class="keyword">if</span>((pte = walk(src, i, <span class="number">0</span>)) == <span class="number">0</span>) <span class="comment">// 找到虚拟地址的最后一级页表项</span></span><br><span class="line">      panic(<span class="string">&quot;kvmcopy: pte should exist&quot;</span>);</span><br><span class="line">    <span class="keyword">if</span>((*pte &amp; PTE_V) == <span class="number">0</span>)	<span class="comment">// 判断页表项是否有效</span></span><br><span class="line">      panic(<span class="string">&quot;kvmcopy: page not present&quot;</span>);</span><br><span class="line">    pa = PTE2PA(*pte);	 <span class="comment">// 将页表项转换为物理地址页起始位置</span></span><br><span class="line">    <span class="comment">// `&amp; ~PTE_U` 表示将该页的权限设置为非用户页</span></span><br><span class="line">    <span class="comment">// 必须设置该权限，RISC-V 中内核是无法直接访问用户页的。</span></span><br><span class="line">    flags = PTE_FLAGS(*pte) &amp; ~PTE_U;</span><br><span class="line">    <span class="comment">// 将pa这一页的PTEs映射到dst上同样的虚拟地址</span></span><br><span class="line">    <span class="keyword">if</span>(mappages(dst, i, PGSIZE, pa, flags) != <span class="number">0</span>)&#123;</span><br><span class="line">      <span class="comment">// 清除已经映射的部分，但不释放内存</span></span><br><span class="line">      uvmunmap(dst, <span class="number">0</span>, i / PGSIZE, <span class="number">0</span>);</span><br><span class="line">      <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 与 uvmdealloc 功能类似，将程序内存从 oldsz 缩减到 newsz。</span></span><br><span class="line"><span class="comment">// 但区别在于不释放实际内存</span></span><br><span class="line"><span class="comment">// 用于内核页表内程序内存映射与用户页表程序内存映射之间的同步</span></span><br><span class="line">uint64</span><br><span class="line"><span class="title function_">kvmdealloc</span><span class="params">(<span class="type">pagetable_t</span> pagetable, uint64 oldsz, uint64 newsz)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">if</span>(newsz &gt;= oldsz)</span><br><span class="line">    <span class="keyword">return</span> oldsz;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">if</span>(PGROUNDUP(newsz) &lt; PGROUNDUP(oldsz))&#123;</span><br><span class="line">    <span class="comment">// 如果存在多余的页需要释放</span></span><br><span class="line">    <span class="type">int</span> npages = (PGROUNDUP(oldsz) - PGROUNDUP(newsz)) / PGSIZE;</span><br><span class="line">    uvmunmap(pagetable, PGROUNDUP(newsz), npages, <span class="number">0</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> newsz;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后，将函数声明在defs.h中。</p>
<hr>
<p>3.修改<code>fork()</code>、<code>exec()</code>和<code>growproc()</code></p>
<p>把fork出来的子进程的用户pagetable复制给kernel_pagetable。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// kernel/proc.c</span></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">fork</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// Copy user memory from parent to child. </span></span><br><span class="line">  <span class="comment">// 将新进程的用户页表映射拷贝一份到新进程的内核页表中</span></span><br><span class="line">  <span class="keyword">if</span>(uvmcopy(p-&gt;pagetable, np-&gt;pagetable, p-&gt;sz) &lt; <span class="number">0</span> ||</span><br><span class="line">     kvmcopy(np-&gt;pagetable, np-&gt;kernel_pagetable, <span class="number">0</span>, p-&gt;sz) &lt; <span class="number">0</span>)&#123;</span><br><span class="line">    freeproc(np);</span><br><span class="line">    release(&amp;np-&gt;lock);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  np-&gt;sz = p-&gt;sz;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在映射之前要先检测程序大小是否超过PLIC，防止remap。<br>同时，映射前要先清除[0，PLIC]中原本的内容，再将要执行的程序映射到[0，PLIC]中。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// kernel/exec.c</span></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">exec</span><span class="params">(<span class="type">char</span> *path, <span class="type">char</span> **argv)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">  <span class="comment">// Load program into memory.</span></span><br><span class="line">  <span class="keyword">for</span>(i=<span class="number">0</span>, off=elf.phoff; i&lt;elf.phnum; i++, off+=<span class="keyword">sizeof</span>(ph))&#123;</span><br><span class="line">    <span class="keyword">if</span>(readi(ip, <span class="number">0</span>, (uint64)&amp;ph, off, <span class="keyword">sizeof</span>(ph)) != <span class="keyword">sizeof</span>(ph))</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">    <span class="keyword">if</span>(ph.type != ELF_PROG_LOAD)</span><br><span class="line">      <span class="keyword">continue</span>;</span><br><span class="line">    <span class="keyword">if</span>(ph.memsz &lt; ph.filesz)</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">    <span class="keyword">if</span>(ph.vaddr + ph.memsz &lt; ph.vaddr)</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">    uint64 sz1;</span><br><span class="line">    <span class="keyword">if</span>((sz1 = uvmalloc(pagetable, sz, ph.vaddr + ph.memsz)) == <span class="number">0</span>)</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">    <span class="comment">// 添加检测，防止程序大小超过 PLIC</span></span><br><span class="line">    <span class="keyword">if</span>(sz1 &gt;= PLIC) &#123;</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">    &#125;</span><br><span class="line">    sz = sz1;</span><br><span class="line">    <span class="keyword">if</span>(ph.vaddr % PGSIZE != <span class="number">0</span>)</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">    <span class="keyword">if</span>(loadseg(pagetable, ph.vaddr, ip, ph.off, ph.filesz) &lt; <span class="number">0</span>)</span><br><span class="line">      <span class="keyword">goto</span> bad;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// Save program name for debugging.</span></span><br><span class="line">  <span class="keyword">for</span>(last=s=path; *s; s++)</span><br><span class="line">    <span class="keyword">if</span>(*s == <span class="string">&#x27;/&#x27;</span>)</span><br><span class="line">      last = s+<span class="number">1</span>;</span><br><span class="line">  safestrcpy(p-&gt;name, last, <span class="keyword">sizeof</span>(p-&gt;name));</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 清除内核页表中对程序内存的旧映射，然后重新建立映射。</span></span><br><span class="line">  uvmunmap(p-&gt;kernel_pagetable, <span class="number">0</span>, PGROUNDUP(oldsz)/PGSIZE, <span class="number">0</span>);</span><br><span class="line">  kvmcopy(pagetable, p-&gt;kernel_pagetable, <span class="number">0</span>, sz);</span><br><span class="line">  </span><br><span class="line">  <span class="comment">// Commit to the user image.</span></span><br><span class="line">  oldpagetable = p-&gt;pagetable;</span><br><span class="line">  p-&gt;pagetable = pagetable;</span><br><span class="line">  p-&gt;sz = sz;</span><br><span class="line">  p-&gt;trapframe-&gt;epc = elf.entry;  <span class="comment">// initial program counter = main</span></span><br><span class="line">  p-&gt;trapframe-&gt;sp = sp; <span class="comment">// initial stack pointer</span></span><br><span class="line">  proc_freepagetable(oldpagetable, oldsz);</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在<code>sysproc.c</code>中的<code>sys_sbrk()</code>中可以发现，执行内存相关的函数为<code>growproc()</code>，所以我们对<code>growproc()</code>进行修改。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// kernel/proc.c</span></span><br><span class="line"><span class="type">int</span></span><br><span class="line"><span class="title function_">growproc</span><span class="params">(<span class="type">int</span> n)</span></span><br><span class="line">&#123;</span><br><span class="line">  uint sz;</span><br><span class="line">  <span class="class"><span class="keyword">struct</span> <span class="title">proc</span> *<span class="title">p</span> =</span> myproc();</span><br><span class="line"></span><br><span class="line">  sz = p-&gt;sz;</span><br><span class="line">  <span class="keyword">if</span>(n &gt; <span class="number">0</span>)&#123;</span><br><span class="line">    uint64 newsz;</span><br><span class="line">    <span class="keyword">if</span>((newsz = uvmalloc(p-&gt;pagetable, sz, sz + n)) == <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 内核页表中的映射同步扩大</span></span><br><span class="line">    <span class="keyword">if</span>(kvmcopy(p-&gt;pagetable, p-&gt;kernel_pagetable, sz, n) != <span class="number">0</span>) &#123;</span><br><span class="line">      <span class="comment">// uvmdealloc(p-&gt;pagetable, newsz, sz);</span></span><br><span class="line">      <span class="keyword">return</span> <span class="number">-1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    sz = newsz;</span><br><span class="line">  &#125; <span class="keyword">else</span> <span class="keyword">if</span>(n &lt; <span class="number">0</span>)&#123;</span><br><span class="line">    uvmdealloc(p-&gt;pagetable, sz, sz + n);</span><br><span class="line">    <span class="comment">// 内核页表中的映射同步缩小</span></span><br><span class="line">    sz = kvmdealloc(p-&gt;kernel_pagetable, sz, sz + n);</span><br><span class="line">  &#125;</span><br><span class="line">  p-&gt;sz = sz;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<p>在userinit的内核页表中包含第一个进程的用户页表</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// kernel/proc.c</span></span><br><span class="line"><span class="comment">// Set up first user process.</span></span><br><span class="line"><span class="type">void</span></span><br><span class="line"><span class="title function_">userinit</span><span class="params">(<span class="type">void</span>)</span></span><br><span class="line">&#123;</span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// allocate one user page and copy init&#x27;s instructions</span></span><br><span class="line">  <span class="comment">// and data into it.</span></span><br><span class="line">  uvminit(p-&gt;pagetable, initcode, <span class="keyword">sizeof</span>(initcode));</span><br><span class="line">  p-&gt;sz = PGSIZE;</span><br><span class="line">  kvmcopy(p-&gt;pagetable, p-&gt;kernel_pagetable, <span class="number">0</span>, p-&gt;sz); <span class="comment">// 同步程序内存映射到进程内核页表中</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// ......</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<hr>
<p>用户页表在用户内核页表中的映射范围为[0，PLIC]，但是从xv6 book中可以看到，<br>全局内核页表的定义中在[0，PLIC]之间存在一个CLINT核心本地中断，<br>CLINT仅在内核启动时使用，所以用户进程内核页表中无需再存在CLINT，<br>所以我们将<code>proc_kvminit()</code>中CLINT映射的部分注释掉，防止再映射用户页表时出现remap</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// vm.c</span></span><br><span class="line"><span class="type">pagetable_t</span></span><br><span class="line"><span class="title function_">proc_kvminit</span><span class="params">()</span> &#123;</span><br><span class="line">  <span class="comment">// 申请一个页表空间</span></span><br><span class="line">  <span class="type">pagetable_t</span> proc_kernel_pagetable = (<span class="type">pagetable_t</span>) kalloc();</span><br><span class="line">  <span class="keyword">if</span> (proc_kernel_pagetable == <span class="number">0</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  <span class="built_in">memset</span>(proc_kernel_pagetable, <span class="number">0</span>, PGSIZE);</span><br><span class="line">  <span class="comment">// 与vminint内容上保持一致</span></span><br><span class="line">  ukvmmap(proc_kernel_pagetable, UART0, UART0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">  <span class="comment">// 用户进程内核页表无需在映射CLINT，将空间留出映射用户页表</span></span><br><span class="line">  <span class="comment">// ukvmmap(proc_kernel_pagetable, CLINT, CLINT, 0x10000, PTE_R | PTE_W);</span></span><br><span class="line">  ukvmmap(proc_kernel_pagetable, PLIC, PLIC, <span class="number">0x400000</span>, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, KERNBASE, KERNBASE, (uint64)etext-KERNBASE, PTE_R | PTE_X);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, (uint64)etext, (uint64)etext, PHYSTOP-(uint64)etext, PTE_R | PTE_W);</span><br><span class="line">  ukvmmap(proc_kernel_pagetable, TRAMPOLINE, (uint64)trampoline, PGSIZE, PTE_R | PTE_X);</span><br><span class="line">  <span class="keyword">return</span> proc_kernel_pagetable;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="遇到的错误"><a href="#遇到的错误" class="headerlink" title="遇到的错误"></a>遇到的错误</h2><blockquote>
<p>缺少页表映射可能会导致内核遇到页面错误。<br>它将打印一个包含sepc=0x00000000XXXXXXXX的错误。<br>你可以通过在kernel/kernel.asm中搜索XXXXXXXX来找出故障发生的位置。</p>
</blockquote>
<hr>
<p>如果出现<code>== Test usertests == Timeout! (300.2s)</code><br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">grand@Lubuntu ~/xv6-labs-2020 (pgtbl)&gt; python3 grade-lab-pgtbl</span><br><span class="line">make: <span class="string">&#x27;kernel/kernel&#x27;</span> is up to <span class="built_in">date</span>.</span><br><span class="line">== Test pte printout == pte printout: OK (3.1s) </span><br><span class="line">== Test answers-pgtbl.txt == answers-pgtbl.txt: FAIL </span><br><span class="line">    answers-pgtbl.txt does not seem to contain enough text</span><br><span class="line">== Test count copyin == count copyin: OK (2.2s) </span><br><span class="line">== Test usertests == Timeout! (300.2s) </span><br><span class="line">== Test   usertests: copyin == </span><br><span class="line">  usertests: copyin: OK </span><br><span class="line">== Test   usertests: copyinstr1 == </span><br><span class="line">  usertests: copyinstr1: OK </span><br><span class="line">== Test   usertests: copyinstr2 == </span><br><span class="line">  usertests: copyinstr2: OK </span><br><span class="line">== Test   usertests: copyinstr3 == </span><br><span class="line">  usertests: copyinstr3: OK </span><br><span class="line">== Test   usertests: sbrkmuch == </span><br><span class="line">  usertests: sbrkmuch: OK </span><br><span class="line">== Test   usertests: all tests == </span><br><span class="line">  usertests: all tests: FAIL </span><br><span class="line">    ...</span><br><span class="line">         <span class="built_in">test</span> bigfile: OK</span><br><span class="line">         <span class="built_in">test</span> dirfile: OK</span><br><span class="line">         <span class="built_in">test</span> iref: OK</span><br><span class="line">         <span class="built_in">test</span> forktest: OK</span><br><span class="line">         <span class="built_in">test</span> bigdir: qemu-system-riscv64: terminating on signal 15 from pid 17227 (make)</span><br><span class="line">    MISSING <span class="string">&#x27;^ALL TESTS PASSED$&#x27;</span></span><br><span class="line">== Test time == </span><br><span class="line">time: OK </span><br><span class="line">Score: 41/66</span><br></pre></td></tr></table></figure><br>就去修改grade-lab-pgtbl，把timeout往大改，毕竟在虚拟机运行会比较慢<br><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@test(<span class="params"><span class="number">0</span>, <span class="string">&quot;usertests&quot;</span></span>)</span></span><br><span class="line"><span class="keyword">def</span> <span class="title function_">test_usertests</span>():</span><br><span class="line">    r.run_qemu(shell_script([</span><br><span class="line">        <span class="string">&#x27;usertests&#x27;</span></span><br><span class="line">    ]), timeout=<span class="number">600</span>) <span class="comment"># 把时间改长，300s-&gt;600s</span></span><br></pre></td></tr></table></figure></p>
<h2 id="实验评分"><a href="#实验评分" class="headerlink" title="实验评分"></a>实验评分</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">grand@Lubuntu ~/xv6-labs-2020 (pgtbl)&gt; python3 grade-lab-pgtbl</span><br><span class="line"></span><br><span class="line">== Test pte printout == pte printout: OK (4.1s) </span><br><span class="line">== Test answers-pgtbl.txt == answers-pgtbl.txt: FAIL </span><br><span class="line">    answers-pgtbl.txt does not seem to contain enough text</span><br><span class="line">== Test count copyin == count copyin: OK (2.4s) </span><br><span class="line">== Test usertests == (284.1s) </span><br><span class="line">== Test   usertests: copyin == </span><br><span class="line">  usertests: copyin: OK </span><br><span class="line">== Test   usertests: copyinstr1 == </span><br><span class="line">  usertests: copyinstr1: OK </span><br><span class="line">== Test   usertests: copyinstr2 == </span><br><span class="line">  usertests: copyinstr2: OK </span><br><span class="line">== Test   usertests: copyinstr3 == </span><br><span class="line">  usertests: copyinstr3: OK </span><br><span class="line">== Test   usertests: sbrkmuch == </span><br><span class="line">  usertests: sbrkmuch: OK </span><br><span class="line">== Test   usertests: all tests == </span><br><span class="line">  usertests: all tests: OK </span><br><span class="line">== Test time == </span><br><span class="line">time: OK </span><br><span class="line">Score: 61/66</span><br></pre></td></tr></table></figure>
<h2 id="修改记录"><a href="#修改记录" class="headerlink" title="修改记录"></a>修改记录</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">grand@Lubuntu ~/xv6-labs-2020 (pgtbl)&gt; git diff origin/pgtbl pgtbl --<span class="built_in">stat</span></span><br><span class="line"> answers-pgtbl.txt |   0</span><br><span class="line"> grade-lab-pgtbl   |   2 +-</span><br><span class="line"> kernel/defs.h     |  13 ++++++++++++</span><br><span class="line"> kernel/exec.c     |  14 ++++++++++++-</span><br><span class="line"> kernel/proc.c     |  86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----</span><br><span class="line"> kernel/proc.h     |   1 +</span><br><span class="line"> kernel/vm.c       | 122 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-</span><br><span class="line"> time.txt          |   1 +</span><br><span class="line"> 8 files changed, 232 insertions(+), 7 deletions(-)</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br><span class="line">372</span><br><span class="line">373</span><br><span class="line">374</span><br><span class="line">375</span><br><span class="line">376</span><br><span class="line">377</span><br><span class="line">378</span><br><span class="line">379</span><br><span class="line">380</span><br><span class="line">381</span><br><span class="line">382</span><br><span class="line">383</span><br><span class="line">384</span><br><span class="line">385</span><br><span class="line">386</span><br><span class="line">387</span><br><span class="line">388</span><br><span class="line">389</span><br><span class="line">390</span><br><span class="line">391</span><br><span class="line">392</span><br><span class="line">393</span><br><span class="line">394</span><br><span class="line">395</span><br><span class="line">396</span><br><span class="line">397</span><br><span class="line">398</span><br><span class="line">399</span><br><span class="line">400</span><br><span class="line">401</span><br><span class="line">402</span><br><span class="line">403</span><br><span class="line">404</span><br><span class="line">405</span><br><span class="line">406</span><br><span class="line">407</span><br><span class="line">408</span><br><span class="line">409</span><br><span class="line">410</span><br><span class="line">411</span><br><span class="line">412</span><br><span class="line">413</span><br><span class="line">414</span><br><span class="line">415</span><br><span class="line">416</span><br><span class="line">417</span><br><span class="line">418</span><br><span class="line">419</span><br><span class="line">420</span><br><span class="line">421</span><br><span class="line">422</span><br><span class="line">423</span><br><span class="line">424</span><br><span class="line">425</span><br><span class="line">426</span><br><span class="line">427</span><br><span class="line">428</span><br><span class="line">429</span><br><span class="line">430</span><br><span class="line">431</span><br><span class="line">432</span><br><span class="line">433</span><br><span class="line">434</span><br><span class="line">435</span><br><span class="line">436</span><br><span class="line">437</span><br><span class="line">438</span><br><span class="line">439</span><br><span class="line">440</span><br><span class="line">441</span><br><span class="line">442</span><br><span class="line">443</span><br><span class="line">444</span><br><span class="line">445</span><br><span class="line">446</span><br><span class="line">447</span><br><span class="line">448</span><br><span class="line">449</span><br><span class="line">450</span><br><span class="line">451</span><br></pre></td><td class="code"><pre><span class="line">diff --git a/answers-pgtbl.txt b/answers-pgtbl.txt</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..e69de29</span><br><span class="line">diff --git a/grade-lab-pgtbl b/grade-lab-pgtbl</span><br><span class="line">index 2b0b49d..bf5a47e 100755</span><br><span class="line">--- a/grade-lab-pgtbl</span><br><span class="line">+++ b/grade-lab-pgtbl</span><br><span class="line">@@ -62,7 +62,7 @@ def test_count():</span><br><span class="line"> def test_usertests():</span><br><span class="line">     r.run_qemu(shell_script([</span><br><span class="line">         &#x27;usertests&#x27;</span><br><span class="line">-    ]), timeout=300)</span><br><span class="line">+    ]), timeout=600) # 把时间改长，300s-&gt;600s</span><br><span class="line"> </span><br><span class="line"> def usertest_check(testcase, nextcase, output):</span><br><span class="line">     if not re.search(r&#x27;\ntest &#123;&#125;: [\s\S]*OK\ntest &#123;&#125;&#x27;.format(testcase, nextcase), output):</span><br><span class="line">diff --git a/kernel/defs.h b/kernel/defs.h</span><br><span class="line">index a73b4f7..4996b44 100644</span><br><span class="line">--- a/kernel/defs.h</span><br><span class="line">+++ b/kernel/defs.h</span><br><span class="line">@@ -108,6 +108,7 @@ void            yield(void);</span><br><span class="line"> int             either_copyout(int user_dst, uint64 dst, void *src, uint64 len);</span><br><span class="line"> int             either_copyin(void *dst, int user_src, uint64 src, uint64 len);</span><br><span class="line"> void            procdump(void);</span><br><span class="line">+void            proc_freekernelpagetable(pagetable_t);</span><br><span class="line"> </span><br><span class="line"> // swtch.S</span><br><span class="line"> void            swtch(struct context*, struct context*);</span><br><span class="line">@@ -167,17 +168,25 @@ pagetable_t     uvmcreate(void);</span><br><span class="line"> void            uvminit(pagetable_t, uchar *, uint);</span><br><span class="line"> uint64          uvmalloc(pagetable_t, uint64, uint64);</span><br><span class="line"> uint64          uvmdealloc(pagetable_t, uint64, uint64);</span><br><span class="line">+</span><br><span class="line"> #ifdef SOL_COW</span><br><span class="line"> #else</span><br><span class="line"> int             uvmcopy(pagetable_t, pagetable_t, uint64);</span><br><span class="line"> #endif</span><br><span class="line">+</span><br><span class="line"> void            uvmfree(pagetable_t, uint64);</span><br><span class="line"> void            uvmunmap(pagetable_t, uint64, uint64, int);</span><br><span class="line"> void            uvmclear(pagetable_t, uint64);</span><br><span class="line">+pte_t*          walk(pagetable_t, uint64, int);</span><br><span class="line"> uint64          walkaddr(pagetable_t, uint64);</span><br><span class="line"> int             copyout(pagetable_t, uint64, char *, uint64);</span><br><span class="line"> int             copyin(pagetable_t, char *, uint64, uint64);</span><br><span class="line"> int             copyinstr(pagetable_t, char *, uint64, uint64);</span><br><span class="line">+void            vmprint(pagetable_t);</span><br><span class="line">+void            ukvmmap(pagetable_t, uint64, uint64, uint64, int);</span><br><span class="line">+pagetable_t     proc_kvminit(void);</span><br><span class="line">+int             kvmcopy(pagetable_t, pagetable_t, uint64, uint64);</span><br><span class="line">+uint64          kvmdealloc(pagetable_t, uint64, uint64);</span><br><span class="line"> </span><br><span class="line"> // plic.c</span><br><span class="line"> void            plicinit(void);</span><br><span class="line">@@ -202,6 +211,10 @@ void            statsinc(void);</span><br><span class="line"> // sprintf.c</span><br><span class="line"> int             snprintf(char*, int, char*, ...);</span><br><span class="line"> </span><br><span class="line">+// vmcopyin.c</span><br><span class="line">+int             copyin_new(pagetable_t, char*, uint64, uint64);</span><br><span class="line">+int             copyinstr_new(pagetable_t, char*, uint64, uint64);</span><br><span class="line">+</span><br><span class="line"> #ifdef LAB_NET</span><br><span class="line"> // pci.c</span><br><span class="line"> void            pci_init();</span><br><span class="line">diff --git a/kernel/exec.c b/kernel/exec.c</span><br><span class="line">index 0e8762f..7577797 100644</span><br><span class="line">--- a/kernel/exec.c</span><br><span class="line">+++ b/kernel/exec.c</span><br><span class="line">@@ -51,6 +51,11 @@ exec(char *path, char **argv)</span><br><span class="line">     uint64 sz1;</span><br><span class="line">     if((sz1 = uvmalloc(pagetable, sz, ph.vaddr + ph.memsz)) == 0)</span><br><span class="line">       goto bad;</span><br><span class="line">+    // 添加检测，防止程序大小超过 PLIC</span><br><span class="line">+    if(sz1 &gt;= PLIC) &#123;</span><br><span class="line">+      goto bad;</span><br><span class="line">+    &#125;</span><br><span class="line">+</span><br><span class="line">     sz = sz1;</span><br><span class="line">     if(ph.vaddr % PGSIZE != 0)</span><br><span class="line">       goto bad;</span><br><span class="line">@@ -107,7 +112,11 @@ exec(char *path, char **argv)</span><br><span class="line">     if(*s == &#x27;/&#x27;)</span><br><span class="line">       last = s+1;</span><br><span class="line">   safestrcpy(p-&gt;name, last, sizeof(p-&gt;name));</span><br><span class="line">-    </span><br><span class="line">+</span><br><span class="line">+  // 清除内核页表中对程序内存的旧映射，然后重新建立映射。</span><br><span class="line">+  uvmunmap(p-&gt;kernel_pagetable, 0, PGROUNDUP(oldsz)/PGSIZE, 0);</span><br><span class="line">+  kvmcopy(pagetable, p-&gt;kernel_pagetable, 0, sz);</span><br><span class="line">+</span><br><span class="line">   // Commit to the user image.</span><br><span class="line">   oldpagetable = p-&gt;pagetable;</span><br><span class="line">   p-&gt;pagetable = pagetable;</span><br><span class="line">@@ -116,6 +125,9 @@ exec(char *path, char **argv)</span><br><span class="line">   p-&gt;trapframe-&gt;sp = sp; // initial stack pointer</span><br><span class="line">   proc_freepagetable(oldpagetable, oldsz);</span><br><span class="line"> </span><br><span class="line">+  // print a page table</span><br><span class="line">+  if(p-&gt;pid == 1) vmprint(p-&gt;pagetable);</span><br><span class="line">+</span><br><span class="line">   return argc; // this ends up in a0, the first argument to main(argc, argv)</span><br><span class="line"> </span><br><span class="line">  bad:</span><br><span class="line">diff --git a/kernel/proc.c b/kernel/proc.c</span><br><span class="line">index dab1e1d..d8c0333 100644</span><br><span class="line">--- a/kernel/proc.c</span><br><span class="line">+++ b/kernel/proc.c</span><br><span class="line">@@ -30,7 +30,7 @@ procinit(void)</span><br><span class="line">   initlock(&amp;pid_lock, &quot;nextpid&quot;);</span><br><span class="line">   for(p = proc; p &lt; &amp;proc[NPROC]; p++) &#123;</span><br><span class="line">       initlock(&amp;p-&gt;lock, &quot;proc&quot;);</span><br><span class="line">-</span><br><span class="line">+/*    // move to allocproc</span><br><span class="line">       // Allocate a page for the process&#x27;s kernel stack.</span><br><span class="line">       // Map it high in memory, followed by an invalid</span><br><span class="line">       // guard page.</span><br><span class="line">@@ -40,6 +40,7 @@ procinit(void)</span><br><span class="line">       uint64 va = KSTACK((int) (p - proc));</span><br><span class="line">       kvmmap(va, (uint64)pa, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">       p-&gt;kstack = va;</span><br><span class="line">+*/</span><br><span class="line">   &#125;</span><br><span class="line">   kvminithart();</span><br><span class="line"> &#125;</span><br><span class="line">@@ -121,6 +122,25 @@ found:</span><br><span class="line">     return 0;</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">+  // An empty user kernel page table</span><br><span class="line">+  p-&gt;kernel_pagetable = proc_kvminit();</span><br><span class="line">+  if (p-&gt;kernel_pagetable == 0) &#123;</span><br><span class="line">+    freeproc(p);</span><br><span class="line">+    release(&amp;p-&gt;lock);</span><br><span class="line">+    return 0;</span><br><span class="line">+  &#125;</span><br><span class="line">+</span><br><span class="line">+  // Allocate a page for the process&#x27;s kernel stack.</span><br><span class="line">+  // Map it high in memory, followed by an invalid</span><br><span class="line">+  // guard page.</span><br><span class="line">+  char *pa = kalloc();</span><br><span class="line">+  if(pa == 0)</span><br><span class="line">+    panic(&quot;kalloc&quot;);</span><br><span class="line">+  uint64 va = KSTACK((int) (p - proc));</span><br><span class="line">+  // kvmmap(va, (uint64)pa, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">+  ukvmmap(p-&gt;kernel_pagetable, va, (uint64)pa, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">+  p-&gt;kstack = va;</span><br><span class="line">+</span><br><span class="line">   // Set up new context to start executing at forkret,</span><br><span class="line">   // which returns to user space.</span><br><span class="line">   memset(&amp;p-&gt;context, 0, sizeof(p-&gt;context));</span><br><span class="line">@@ -139,9 +159,27 @@ freeproc(struct proc *p)</span><br><span class="line">   if(p-&gt;trapframe)</span><br><span class="line">     kfree((void*)p-&gt;trapframe);</span><br><span class="line">   p-&gt;trapframe = 0;</span><br><span class="line">+</span><br><span class="line">+  // 删除内核栈</span><br><span class="line">+  if(p-&gt;kstack) &#123;</span><br><span class="line">+    // 通过页表地址， kstack虚拟地址 找到最后一级的页表项</span><br><span class="line">+    pte_t* pte = walk(p-&gt;kernel_pagetable, p-&gt;kstack, 0);</span><br><span class="line">+    if (pte == 0)</span><br><span class="line">+      panic(&quot;freeproc : kstack&quot;);</span><br><span class="line">+    // 删除页表项对应的物理地址</span><br><span class="line">+    kfree((void*)PTE2PA(*pte));</span><br><span class="line">+  &#125;</span><br><span class="line">+  p-&gt;kstack = 0;</span><br><span class="line">+</span><br><span class="line">   if(p-&gt;pagetable)</span><br><span class="line">     proc_freepagetable(p-&gt;pagetable, p-&gt;sz);</span><br><span class="line">   p-&gt;pagetable = 0;</span><br><span class="line">+</span><br><span class="line">+  // 删除kernel pagetable</span><br><span class="line">+  if(p-&gt;kernel_pagetable) </span><br><span class="line">+    proc_freekernelpagetable(p-&gt;kernel_pagetable);</span><br><span class="line">+  p-&gt;kernel_pagetable = 0;</span><br><span class="line">+</span><br><span class="line">   p-&gt;sz = 0;</span><br><span class="line">   p-&gt;pid = 0;</span><br><span class="line">   p-&gt;parent = 0;</span><br><span class="line">@@ -195,6 +233,23 @@ proc_freepagetable(pagetable_t pagetable, uint64 sz)</span><br><span class="line">   uvmfree(pagetable, sz);</span><br><span class="line"> &#125;</span><br><span class="line"> </span><br><span class="line">+// Free a process&#x27;s kernel page tabel</span><br><span class="line">+void </span><br><span class="line">+proc_freekernelpagetable(pagetable_t pagetable)</span><br><span class="line">+&#123;</span><br><span class="line">+  for(int i = 0; i &lt; 512; i++) &#123;</span><br><span class="line">+    pte_t pte = pagetable[i];</span><br><span class="line">+    if(pte &amp; PTE_V) &#123;</span><br><span class="line">+      pagetable[i] = 0;</span><br><span class="line">+      if((pte &amp; (PTE_R | PTE_W | PTE_X)) == 0) &#123;</span><br><span class="line">+        uint64 child = PTE2PA(pte);</span><br><span class="line">+        proc_freekernelpagetable((pagetable_t)child);</span><br><span class="line">+      &#125;</span><br><span class="line">+    &#125;</span><br><span class="line">+  &#125;</span><br><span class="line">+  kfree((void*)pagetable);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line"> // a user program that calls exec(&quot;/init&quot;)</span><br><span class="line"> // od -t xC initcode</span><br><span class="line"> uchar initcode[] = &#123;</span><br><span class="line">@@ -220,6 +275,8 @@ userinit(void)</span><br><span class="line">   // and data into it.</span><br><span class="line">   uvminit(p-&gt;pagetable, initcode, sizeof(initcode));</span><br><span class="line">   p-&gt;sz = PGSIZE;</span><br><span class="line">+  // 同步程序内存映射到进程内核页表中</span><br><span class="line">+  kvmcopy(p-&gt;pagetable, p-&gt;kernel_pagetable, 0, p-&gt;sz);</span><br><span class="line"> </span><br><span class="line">   // prepare for the very first &quot;return&quot; from kernel to user.</span><br><span class="line">   p-&gt;trapframe-&gt;epc = 0;      // user program counter</span><br><span class="line">@@ -242,12 +299,23 @@ growproc(int n)</span><br><span class="line">   struct proc *p = myproc();</span><br><span class="line"> </span><br><span class="line">   sz = p-&gt;sz;</span><br><span class="line">-  if(n &gt; 0)&#123;</span><br><span class="line">+  if(n &gt; 0) &#123;</span><br><span class="line">+    if(PGROUNDDOWN(sz + n) &gt;= PLIC)</span><br><span class="line">+      return -1;</span><br><span class="line">+</span><br><span class="line">     if((sz = uvmalloc(p-&gt;pagetable, sz, sz + n)) == 0) &#123;</span><br><span class="line">       return -1;</span><br><span class="line">     &#125;</span><br><span class="line">-  &#125; else if(n &lt; 0)&#123;</span><br><span class="line">+</span><br><span class="line">+    // 内核页表中的映射同步扩大</span><br><span class="line">+    if(kvmcopy(p-&gt;pagetable, p-&gt;kernel_pagetable, p-&gt;sz, sz) == -1) &#123;</span><br><span class="line">+      return -1;</span><br><span class="line">+    &#125;</span><br><span class="line">+    </span><br><span class="line">+  &#125; else if(n &lt; 0) &#123;</span><br><span class="line">     sz = uvmdealloc(p-&gt;pagetable, sz, sz + n);</span><br><span class="line">+    // 内核页表中的映射同步缩小</span><br><span class="line">+    kvmdealloc(p-&gt;kernel_pagetable, p-&gt;sz, p-&gt;sz + n);</span><br><span class="line">   &#125;</span><br><span class="line">   p-&gt;sz = sz;</span><br><span class="line">   return 0;</span><br><span class="line">@@ -268,7 +336,8 @@ fork(void)</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">   // Copy user memory from parent to child.</span><br><span class="line">-  if(uvmcopy(p-&gt;pagetable, np-&gt;pagetable, p-&gt;sz) &lt; 0)&#123;</span><br><span class="line">+  if(uvmcopy(p-&gt;pagetable, np-&gt;pagetable, p-&gt;sz) &lt; 0 ||</span><br><span class="line">+     kvmcopy(np-&gt;pagetable, np-&gt;kernel_pagetable, 0, p-&gt;sz) &lt; 0) &#123;</span><br><span class="line">     freeproc(np);</span><br><span class="line">     release(&amp;np-&gt;lock);</span><br><span class="line">     return -1;</span><br><span class="line">@@ -473,8 +542,17 @@ scheduler(void)</span><br><span class="line">         // before jumping back to us.</span><br><span class="line">         p-&gt;state = RUNNING;</span><br><span class="line">         c-&gt;proc = p;</span><br><span class="line">+</span><br><span class="line">+        // 在切换任务前，将用户内核页表替换到stap寄存器中</span><br><span class="line">+        w_satp(MAKE_SATP(p-&gt;kernel_pagetable));</span><br><span class="line">+        // 清除快表缓存</span><br><span class="line">+        sfence_vma();</span><br><span class="line">+</span><br><span class="line">         swtch(&amp;c-&gt;context, &amp;p-&gt;context);</span><br><span class="line"> </span><br><span class="line">+        // 该进程执行结束后，将SATP寄存器的值设置为全局内核页表地址</span><br><span class="line">+        kvminithart();</span><br><span class="line">+</span><br><span class="line">         // Process is done running for now.</span><br><span class="line">         // It should have changed its p-&gt;state before coming back.</span><br><span class="line">         c-&gt;proc = 0;</span><br><span class="line">diff --git a/kernel/proc.h b/kernel/proc.h</span><br><span class="line">index 9c16ea7..2cdd693 100644</span><br><span class="line">--- a/kernel/proc.h</span><br><span class="line">+++ b/kernel/proc.h</span><br><span class="line">@@ -98,6 +98,7 @@ struct proc &#123;</span><br><span class="line">   uint64 kstack;               // Virtual address of kernel stack</span><br><span class="line">   uint64 sz;                   // Size of process memory (bytes)</span><br><span class="line">   pagetable_t pagetable;       // User page table</span><br><span class="line">+  pagetable_t kernel_pagetable;// kernel page tabel of each process</span><br><span class="line">   struct trapframe *trapframe; // data page for trampoline.S</span><br><span class="line">   struct context context;      // swtch() here to run process</span><br><span class="line">   struct file *ofile[NOFILE];  // Open files</span><br><span class="line">diff --git a/kernel/vm.c b/kernel/vm.c</span><br><span class="line">index bccb405..3e95042 100644</span><br><span class="line">--- a/kernel/vm.c</span><br><span class="line">+++ b/kernel/vm.c</span><br><span class="line">@@ -5,6 +5,8 @@</span><br><span class="line"> #include &quot;riscv.h&quot;</span><br><span class="line"> #include &quot;defs.h&quot;</span><br><span class="line"> #include &quot;fs.h&quot;</span><br><span class="line">+#include &quot;spinlock.h&quot;</span><br><span class="line">+#include &quot;proc.h&quot;</span><br><span class="line"> </span><br><span class="line"> /*</span><br><span class="line">  * the kernel&#x27;s page table.</span><br><span class="line">@@ -132,7 +134,10 @@ kvmpa(uint64 va)</span><br><span class="line">   pte_t *pte;</span><br><span class="line">   uint64 pa;</span><br><span class="line">   </span><br><span class="line">-  pte = walk(kernel_pagetable, va, 0);</span><br><span class="line">+  // pte = walk(kernel_pagetable, va, 0);</span><br><span class="line">+  // 使用用户进程自己的内核页表地址来翻译虚拟地址</span><br><span class="line">+  pte = walk(myproc()-&gt;kernel_pagetable, va, 0);</span><br><span class="line">+</span><br><span class="line">   if(pte == 0)</span><br><span class="line">     panic(&quot;kvmpa&quot;);</span><br><span class="line">   if((*pte &amp; PTE_V) == 0)</span><br><span class="line">@@ -379,6 +384,7 @@ copyout(pagetable_t pagetable, uint64 dstva, char *src, uint64 len)</span><br><span class="line"> int</span><br><span class="line"> copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)</span><br><span class="line"> &#123;</span><br><span class="line">+/*</span><br><span class="line">   uint64 n, va0, pa0;</span><br><span class="line"> </span><br><span class="line">   while(len &gt; 0)&#123;</span><br><span class="line">@@ -396,6 +402,8 @@ copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)</span><br><span class="line">     srcva = va0 + PGSIZE;</span><br><span class="line">   &#125;</span><br><span class="line">   return 0;</span><br><span class="line">+*/</span><br><span class="line">+  return copyin_new(pagetable, dst, srcva, len);</span><br><span class="line"> &#125;</span><br><span class="line"> </span><br><span class="line"> // Copy a null-terminated string from user to kernel.</span><br><span class="line">@@ -405,6 +413,7 @@ copyin(pagetable_t pagetable, char *dst, uint64 srcva, uint64 len)</span><br><span class="line"> int</span><br><span class="line"> copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)</span><br><span class="line"> &#123;</span><br><span class="line">+/*</span><br><span class="line">   uint64 n, va0, pa0;</span><br><span class="line">   int got_null = 0;</span><br><span class="line"> </span><br><span class="line">@@ -439,4 +448,115 @@ copyinstr(pagetable_t pagetable, char *dst, uint64 srcva, uint64 max)</span><br><span class="line">   &#125; else &#123;</span><br><span class="line">     return -1;</span><br><span class="line">   &#125;</span><br><span class="line">+*/</span><br><span class="line">+  return copyinstr_new(pagetable, dst, srcva, max);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+// vmprint辅助函数</span><br><span class="line">+static void</span><br><span class="line">+_vmprint(pagetable_t pagetable, int level)</span><br><span class="line">+&#123;</span><br><span class="line">+  for (int i = 0; i &lt; 512; ++i) &#123;</span><br><span class="line">+    pte_t pte = pagetable[i];</span><br><span class="line">+</span><br><span class="line">+    if ((pte &amp; PTE_V)) &#123;</span><br><span class="line">+      for (int j = 0; j &lt; level; ++j) &#123;</span><br><span class="line">+        if (j == 0) printf(&quot;..&quot;);</span><br><span class="line">+        else printf(&quot; ..&quot;);</span><br><span class="line">+      &#125;</span><br><span class="line">+</span><br><span class="line">+      uint64 child = PTE2PA(pte); // 通过pte映射下一级页表的物理地址</span><br><span class="line">+      printf(&quot;%d: pte %p pa %p\n&quot;, i, pte, child);</span><br><span class="line">+      // 查看flag是否被设置，若被设置，则为最低一层</span><br><span class="line">+      // 只有在页表的最后一级，才可进行读、写、执行</span><br><span class="line">+      if ((pte &amp; (PTE_R | PTE_W | PTE_X)) == 0)</span><br><span class="line">+        _vmprint((pagetable_t)child, level + 1);</span><br><span class="line">+    &#125;</span><br><span class="line">+  &#125;</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+// Print a page table</span><br><span class="line">+void</span><br><span class="line">+vmprint(pagetable_t pagetable)</span><br><span class="line">+&#123;</span><br><span class="line">+  printf(&quot;page table %p\n&quot;, pagetable);</span><br><span class="line">+  _vmprint(pagetable, 1);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+// map to the process&#x27;s kernel page table</span><br><span class="line">+void</span><br><span class="line">+ukvmmap(pagetable_t pagetable, uint64 va, uint64 pa, uint64 sz, int perm)</span><br><span class="line">+&#123;</span><br><span class="line">+  if(mappages(pagetable, va, sz, pa, perm) != 0)</span><br><span class="line">+    panic(&quot;ukvmmap&quot;);</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+// init the kernel page tabel of user process</span><br><span class="line">+pagetable_t</span><br><span class="line">+proc_kvminit()</span><br><span class="line">+&#123;</span><br><span class="line">+  // 申请一个页表空间</span><br><span class="line">+  pagetable_t proc_kernel_pagetable = (pagetable_t) kalloc();</span><br><span class="line">+  if (proc_kernel_pagetable == 0)</span><br><span class="line">+    return 0;</span><br><span class="line">+  memset(proc_kernel_pagetable, 0, PGSIZE);</span><br><span class="line">+  // 与vminint内容上保持一致</span><br><span class="line">+  ukvmmap(proc_kernel_pagetable, UART0, UART0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">+  ukvmmap(proc_kernel_pagetable, VIRTIO0, VIRTIO0, PGSIZE, PTE_R | PTE_W);</span><br><span class="line">+  // 用户进程内核页表无需在映射CLINT，将空间留出映射用户页表</span><br><span class="line">+  // ukvmmap(proc_kernel_pagetable, CLINT, CLINT, 0x10000, PTE_R | PTE_W);</span><br><span class="line">+  ukvmmap(proc_kernel_pagetable, PLIC, PLIC, 0x400000, PTE_R | PTE_W);</span><br><span class="line">+  ukvmmap(proc_kernel_pagetable, KERNBASE, KERNBASE, (uint64)etext-KERNBASE, PTE_R | PTE_X);</span><br><span class="line">+  ukvmmap(proc_kernel_pagetable, (uint64)etext, (uint64)etext, PHYSTOP-(uint64)etext, PTE_R | PTE_W);</span><br><span class="line">+  ukvmmap(proc_kernel_pagetable, TRAMPOLINE, (uint64)trampoline, PGSIZE, PTE_R | PTE_X);</span><br><span class="line">+  return proc_kernel_pagetable;</span><br><span class="line"> &#125;</span><br><span class="line">+</span><br><span class="line">+// 将 src 页表的一部分页映射关系拷贝到 dst 页表中。</span><br><span class="line">+// 只拷贝页表项，不拷贝实际的物理页内存。</span><br><span class="line">+// 成功返回0，失败返回 -1</span><br><span class="line">+int</span><br><span class="line">+kvmcopy(pagetable_t src, pagetable_t dst, uint64 start, uint64 end)</span><br><span class="line">+&#123;</span><br><span class="line">+  pte_t *pte;</span><br><span class="line">+</span><br><span class="line">+  // PGROUNDUP: 对齐页边界，防止 remap</span><br><span class="line">+  for(uint64 i = PGROUNDUP(start); i &lt; end; i += PGSIZE) &#123;</span><br><span class="line">+    if((pte = walk(src, i, 0)) == 0) // 找到虚拟地址的最后一级页表项</span><br><span class="line">+      panic(&quot;kvmcopy: pte should exist&quot;);</span><br><span class="line">+    if((*pte &amp; PTE_V) == 0)	      // 判断页表项是否有效</span><br><span class="line">+      panic(&quot;kvmcopy: page not present&quot;);</span><br><span class="line">+</span><br><span class="line">+    // 将页表项转换为物理地址页起始位置</span><br><span class="line">+    uint64 pa = PTE2PA(*pte);</span><br><span class="line">+    // `&amp; ~PTE_U` 表示将该页的权限设置为非用户页</span><br><span class="line">+    // 必须设置该权限，RISC-V 中内核是无法直接访问用户页的。</span><br><span class="line">+    uint flags = PTE_FLAGS(*pte) &amp; ~PTE_U;</span><br><span class="line">+    // 将pa这一页的PTEs映射到dst上同样的虚拟地址</span><br><span class="line">+    if(mappages(dst, i, PGSIZE, pa, flags) != 0) &#123;</span><br><span class="line">+      // 清除已经映射的部分，但不释放内存</span><br><span class="line">+      uvmunmap(dst, 0, i / PGSIZE, 0);</span><br><span class="line">+      return -1;</span><br><span class="line">+    &#125;</span><br><span class="line">+  &#125;</span><br><span class="line">+  return 0;</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+// 与 uvmdealloc 功能类似，将程序内存从 oldsz 缩减到 newsz。但区别在于不释放实际内存</span><br><span class="line">+// 用于内核页表内程序内存映射与用户页表程序内存映射之间的同步</span><br><span class="line">+uint64</span><br><span class="line">+kvmdealloc(pagetable_t pagetable, uint64 oldsz, uint64 newsz)</span><br><span class="line">+&#123;</span><br><span class="line">+  if(newsz &gt;= oldsz)</span><br><span class="line">+    return oldsz;</span><br><span class="line">+</span><br><span class="line">+  if(PGROUNDUP(newsz) &lt; PGROUNDUP(oldsz))&#123;</span><br><span class="line">+    // 如果存在多余的页需要释放</span><br><span class="line">+    int npages = (PGROUNDUP(oldsz) - PGROUNDUP(newsz)) / PGSIZE;</span><br><span class="line">+    uvmunmap(pagetable, PGROUNDUP(newsz), npages, 0);</span><br><span class="line">+  &#125;</span><br><span class="line">+</span><br><span class="line">+  return newsz;</span><br><span class="line">+&#125;</span><br><span class="line">+</span><br><span class="line">+</span><br><span class="line">diff --git a/time.txt b/time.txt</span><br><span class="line">new file mode 100644</span><br><span class="line">index 0000000..a45fd52</span><br><span class="line">--- /dev/null</span><br><span class="line">+++ b/time.txt</span><br><span class="line">@@ -0,0 +1 @@</span><br><span class="line">+24</span><br></pre></td></tr></table></figure></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io">kiloGrand</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://kilogrand.gitee.io/2022/09/03/xv6-lab3/">https://kilogrand.gitee.io/2022/09/03/xv6-lab3/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://kilogrand.gitee.io" target="_blank">kiloGrand</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/xv6/">xv6</a></div><div class="post_share"></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/09/02/xv6-lab2/"><img class="prev-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">xv6 实验2 system calls</div></div></a></div><div class="next-post pull-right"><a href="/2022/09/04/xv6-lab4/"><img class="next-cover" src="/img/coding.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">xv6 实验4 Traps</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/08/31/xv6-Chapter-0/" title="xv6 riscv book 中文版声明"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 riscv book 中文版声明</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-1/" title="xv6 第一章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第一章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-2/" title="xv6 第二章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第二章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-3/" title="xv6 第三章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第三章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-4/" title="xv6 第四章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第四章</div></div></a></div><div><a href="/2022/08/31/xv6-Chapter-5/" title="xv6 第五章"><img class="cover" src="/img/coding.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-08-31</div><div class="title">xv6 第五章</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/profile.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">kiloGrand</div><div class="author-info__description">coder && data-science researcher</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">46</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">6</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">5</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/kiloGrand/"><i class="fab fa-github"></i><span>Follow Me</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Lab-pgtbl-Page-tables"><span class="toc-number">1.</span> <span class="toc-text">Lab pgtbl: Page tables</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#Print-a-page-table"><span class="toc-number">1.1.</span> <span class="toc-text">Print a page table</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#A-kernel-page-table-per-process"><span class="toc-number">1.2.</span> <span class="toc-text">A kernel page table per process</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%80%E4%B8%AA%E5%86%85%E6%A0%B8%E9%A1%B5%E8%A1%A8%E7%9A%84%E5%88%9B%E5%BB%BA"><span class="toc-number">1.2.1.</span> <span class="toc-text">一个内核页表的创建</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%BA%E6%AF%8F%E4%B8%AA%E6%96%B0%E8%BF%9B%E7%A8%8B%E7%94%9F%E6%88%90%E4%B8%80%E4%B8%AA%E5%86%85%E6%A0%B8%E9%A1%B5%E8%A1%A8"><span class="toc-number">1.2.2.</span> <span class="toc-text">为每个新进程生成一个内核页表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%87%E6%8D%A2%E8%BF%9B%E7%A8%8B%E6%97%B6%E8%83%BD%E5%A4%9F%E5%88%87%E6%8D%A2%E5%86%85%E6%A0%B8%E9%A1%B5%E8%A1%A8"><span class="toc-number">1.2.3.</span> <span class="toc-text">切换进程时能够切换内核页表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%94%80%E6%AF%81%E8%BF%9B%E7%A8%8B%E6%97%B6%E9%87%8A%E6%94%BE%E5%86%85%E6%A0%B8%E9%A1%B5%E8%A1%A8"><span class="toc-number">1.2.4.</span> <span class="toc-text">销毁进程时释放内核页表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E8%BF%9B%E7%A8%8B%E5%86%85%E6%A0%B8%E9%A1%B5%E8%A1%A8"><span class="toc-number">1.2.5.</span> <span class="toc-text">获取进程内核页表</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Simplify-copyin-copyinstr"><span class="toc-number">1.3.</span> <span class="toc-text">Simplify copyin&#x2F;copyinstr</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%81%87%E5%88%B0%E7%9A%84%E9%94%99%E8%AF%AF"><span class="toc-number">1.4.</span> <span class="toc-text">遇到的错误</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%9E%E9%AA%8C%E8%AF%84%E5%88%86"><span class="toc-number">1.5.</span> <span class="toc-text">实验评分</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E8%AE%B0%E5%BD%95"><span class="toc-number">1.6.</span> <span class="toc-text">修改记录</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/24/kuiper_infer-L14/" title="自制深度学习框架--实现Yolov5的推理">自制深度学习框架--实现Yolov5的推理</a><time datetime="2023-03-24T12:00:00.000Z" title="发表于 2023-03-24 20:00:00">2023-03-24</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/23/kuiper_infer-L13/" title="自制深度学习框架--实现ResNet网络的推理">自制深度学习框架--实现ResNet网络的推理</a><time datetime="2023-03-23T12:00:00.000Z" title="发表于 2023-03-23 20:00:00">2023-03-23</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/22/kuiper_infer-L12/" title="自制深度学习框架--算子的执行流程">自制深度学习框架--算子的执行流程</a><time datetime="2023-03-22T12:00:00.000Z" title="发表于 2023-03-22 20:00:00">2023-03-22</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/21/kuiper_infer-L11/" title="自制深度学习框架--再探Tensor类并构建计算图的图关系">自制深度学习框架--再探Tensor类并构建计算图的图关系</a><time datetime="2023-03-21T12:00:00.000Z" title="发表于 2023-03-21 20:00:00">2023-03-21</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2023/03/20/kuiper_infer-L10/" title="自制深度学习框架--Im2Col原理与卷积层的实现">自制深度学习框架--Im2Col原理与卷积层的实现</a><time datetime="2023-03-20T12:00:00.000Z" title="发表于 2023-03-20 20:00:00">2023-03-20</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2022 - 2023 By kiloGrand</div><div class="footer_custom_text">Hi, welcome to my blog!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="/js/search/local-search.js"></script><div class="js-pjax"><script>if (!window.MathJax) {
  window.MathJax = {
    tex: {
      inlineMath: [ ['$','$'], ["\\(","\\)"]],
      tags: 'ams'
    },
    chtml: {
      scale: 1.2
    },
    options: {
      renderActions: {
        findScript: [10, doc => {
          for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
            const display = !!node.type.match(/; *mode=display/)
            const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
            const text = document.createTextNode('')
            node.parentNode.replaceChild(text, node)
            math.start = {node: text, delim: '', n: 0}
            math.end = {node: text, delim: '', n: 0}
            doc.math.push(math)
          }
        }, ''],
        insertScript: [200, () => {
          document.querySelectorAll('mjx-container:not\([display]\)').forEach(node => {
            const target = node.parentNode
            if (target.nodeName.toLowerCase() === 'li') {
              target.parentNode.classList.add('has-jax')
            } else {
              target.classList.add('has-jax')
            }
          });
        }, '', false]
      }
    }
  }
  
  const script = document.createElement('script')
  script.src = 'https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js'
  script.id = 'MathJax-script'
  script.async = true
  document.head.appendChild(script)
} else {
  MathJax.startup.document.state(0)
  MathJax.texReset()
  MathJax.typeset()
}</script></div><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>